package org.example.controller;

import org.apache.ibatis.annotations.Param;
import org.example.model.vo.RoleVo;
import org.example.service.RoleService;
import org.example.util.CommonRequest;
import org.example.util.CommonRequestHandle;
import org.example.util.Result;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

@RestController
@RequestMapping("/role")
@CrossOrigin(origins = "*",maxAge = 3600)
public class RoleController {
    @Resource
    RoleService roleService;

    @PostMapping("/add")
    public Result addRole(@RequestHeader("token") String token, @RequestBody CommonRequest commonRequest) {
        RoleVo roleVo = CommonRequestHandle.getRequest(commonRequest, RoleVo.class);
        return roleService.addRole(token, roleVo);
    }

    @PostMapping("/edit")
    public Result editRole(@RequestHeader("token") String token, @RequestBody CommonRequest commonRequest) {
        RoleVo roleVo = CommonRequestHandle.getRequest(commonRequest, RoleVo.class);
        return roleService.editRole(token, roleVo);
    }

    @GetMapping("/getRoleAndMenu")
    public Result getRoleAndMenu(
            @RequestParam(defaultValue = "1", required = false) Integer page,
            @RequestParam(defaultValue = "10", required = false, name = "limit") Integer size,
            RoleVo roleVo
    ) {
        return roleService.getRoleAndMenu(page, size, roleVo);
    }

    @GetMapping("/getRoleList")
    public Result getRoleList(
            @RequestParam(defaultValue = "1", required = false) int page,
            @RequestParam(defaultValue = "10", required = false) int size,
            RoleVo roleVo
    ) {
        return roleService.getPageRole(page, size, roleVo);
    }

    @GetMapping("/switchStatus")
    public Result switchStatus(@Param("id") Long id, @Param("status") Long status){
        return roleService.switchStatusById(id, status);
    }

    @GetMapping("/delRole")
    public Result deleteById(@Param("id") Long id){
        return roleService.deleteById(id);
    }

    @GetMapping("/getRoleCount")
    public Result getRoleCount(){
        return roleService.getRoleCount();
    }
}
